/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import entidades.Reserva;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import negocio.NegocioDAO;
import negocio.NegocioException;
import seguranca.Interfaces.IReserva;

/**
 *
 * @author renan
 */
public class ReservaDao extends NegocioDAO implements IReserva{
    private Reserva reserva;

    public ReservaDao(){
        reserva = new Reserva();
    }

    public void solicitarReserva() throws NegocioException {
        
        reserva.getCliente().setId(UsuarioDAO.id);
        reserva.setDataHora(new Date());
        validar();
        EntityManager persistencia = NegocioDAO.getInstance().getEntityManager();
        persistencia.persist(reserva);
        persistencia.getTransaction().commit();
        persistencia.close();
    }


    public List<Reserva> pesquisar()throws NegocioException{
        List<Reserva> colecao = null;
        EntityManager em = getEntityManager();
        Query re = em.createQuery("SELECT r FROM Reserva r");
        colecao = re.getResultList();
        return colecao;
     }

    private void validar()throws NegocioException{
        erros.limpar();

        if(estaVazio(reserva.getCliente().getId())){
            erros.campoObrigatorio("cliente");
        }

        
        if(erros.existe()){
            throw new NegocioException(erros);
        }
    }
    
    public void limpar(){
        reserva.setCliente(null);
        reserva.setId("");
        
    }

    public Reserva getReserva() {
        return reserva;
    }

    public void setReserva(Reserva reserva) {
        this.reserva = reserva;
    }


}
